package com.innovation.wjrpc.convert;

import com.innovation.wjrpc.codec.Decoder;
import com.innovation.wjrpc.codec.impl.JSONDecoder;
import com.innovation.wjrpc.protocol.Response;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

/**
 * @author innoyiya
 * @date 2021/9/8 11:16
 */
public class TranslateMessage extends MessageToMessageDecoder<ByteBuf> {

    // todo 重构解码器配置
    private Decoder decoder = new JSONDecoder();

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        byte[] bytes = new byte[msg.readableBytes()];
        msg.readBytes(bytes);
        Response response = decoder.decode(bytes, Response.class);
        out.add(response);
    }
}

